<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['applet:appSite:add']">新增
        </el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['applet:appSite:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['applet:appSite:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['applet:appSite:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['applet:appSite:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table border stripe v-loading="loading" :data="appSiteList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="编号" width="80" show-overflow-tooltip align="center" prop="id"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column width="100" label="奖励积分" align="center" prop="rewardGrade"/>
      <el-table-column width="100" label="解锁积分" align="center" prop="grade"/>
      <el-table-column width="100" label="解锁人数" align="center" prop="finishSum"/>
      <el-table-column label="状态" width="100" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="未解锁照片" align="center" prop="lockImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.lockImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="解锁照片" align="center" prop="clearImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.clearImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="顶部照片" align="center" prop="topImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.topImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="视频" width="150" align="center" prop="vedio"/>
      <el-table-column label="提示文案" width="150" show-overflow-tooltip align="center" prop="tipText"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="更新账号" align="center" prop="updateBy" width="120"/>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:appSite:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:appSite:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识关卡对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="解锁积分" prop="grade">
              <el-input-number :min="0" v-model="form.grade" placeholder="请输入解锁需要的积分"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="奖励积分" prop="rewardGrade">
              <el-input-number :min="0" v-model="form.rewardGrade" placeholder="请输入解锁需要的积分"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="未解锁照片" prop="lockImg">
              <image-upload :limit="1" v-model="form.lockImg"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="解锁照片" prop="clearImg">
              <image-upload :limit="1" v-model="form.clearImg"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提示文案" prop="tipText">
              <el-input v-model="form.tipText" placeholder="请输入提示文案"/>
            </el-form-item>
            <el-form-item label="学习视频" prop="vedio">
              <file-upload :limit="1" v-model="form.vedio"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="关卡照片" prop="topImg">
              <image-upload :limit="5" v-model="form.topImg"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="信息简介">
          <editor v-model="form.info" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAppSite,
  changeSiteStatus,
  delAppSite,
  getAppSite,
  listAppSite,
  updateAppSite
} from "@/api/background/appSite";

export default {
  name: "AppSite",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识关卡表格数据
      appSiteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lockImg: [
          {required: true, message: "未解锁照片不能为空", trigger: "blur"}
        ],
        clearImg: [
          {required: true, message: "解锁照片不能为空", trigger: "blur"}
        ],
        topImg: [
          {required: true, message: "顶部照片不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        grade: [
          {required: true, message: "解锁需要的积分不能为空", trigger: "blur"}
        ],
        rewardGrade: [
          {required: true, message: "奖励积分不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatusChange(row) {
      let text = row.status == 0 ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"的关卡吗？').then(function () {
        return changeSiteStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.handleQuery()
      }).catch(function () {
        row.status = row.status == 0 ? "1" : "0";
      });
    },
    /** 查询知识关卡列表 */
    getList() {
      this.loading = true;
      listAppSite(this.queryParams).then(response => {
        this.appSiteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lockImg: null,
        clearImg: null,
        topImg: null,
        name: null,
        info: null,
        vedio: null,
        grade: null,
        rewardGrade:null,
        tipText:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识关卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppSite(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改知识关卡";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppSite(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppSite(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除知识关卡编号为"' + ids + '"的数据项？').then(function () {
        return delAppSite(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/appSite/export', {
        ...this.queryParams
      }, `appSite_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
